package com.example.boot.controller;

import com.example.boot.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


import javax.servlet.http.HttpSession;

/**
 * @author shkstart
 * @creat 2021-04-04-下午4:54
 */
@Controller
public class IndexController {

    @GetMapping({"/","/login"})
    public String loginPage(){
        return "login";
    }
    @PostMapping("/login")
    public String main(User user, HttpSession session){
        if(StringUtils.hasLength(user.getUsername())&&"123456".equals(user.getPassword())){
            session.setAttribute("loginUser",user);

            return "redirect:/main.html";
        }else {
            session.setAttribute("msg","账号密码错误！");
            return "login";
        }

    }
    //templates里面的文件都是通过控制器过来的，不能直接访问，
    // static这四个存放静态文件的可以直接访问
    @GetMapping("/main.html")
    public String mainPage(HttpSession session,Model model){

//        Object loginUser = session.getAttribute("loginUser");
//        if(loginUser!=null){
//            return "main";
//        }else {
//            model.addAttribute("msg","请重新登录！");
//            return "login";
//        }
        return "main";
    }
}
